package com.ogawa.chair7808.ui.dialog

import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.*
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.blankj.utilcode.util.LogUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.ogawa.base.Constant.CommmandNum
import com.ogawa.base.Constant.MqttConstant
import com.ogawa.chair7808.R
import com.ogawa.chair7808.cache.deviceStatusBean
import com.ogawa.chair7808.manager.DataManager
import com.ogawa.chair7808.manager.DataManager.Companion.dataManager
import com.ogawa.chair7808.ui.activity.BaseActivity


class AntiPinchDialog : DialogFragment() {
    companion object{
        var isShowAntiPinchDialog = false
    }

    private lateinit var rootView : View
    private var baseActivity: BaseActivity?=null

    fun setBaseActivity(activity: BaseActivity){
        baseActivity = activity
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        LogUtils.e("AntiPinchDialog onDismiss")
        isShowAntiPinchDialog = false
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        //        getDialog().getWindow().setLayout(DensityUtil.dip2px(getContext(), 400), DensityUtil.dip2px(getContext(), 240));
        // 隐藏标题栏, 不加弹窗上方会一个透明的标题栏占着空间
        dialog!!.requestWindowFeature(Window.FEATURE_NO_TITLE)
        // 必须设置这两个,才能设置宽度
        dialog!!.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog!!.window?.decorView?.setBackgroundColor(Color.TRANSPARENT)
        // 遮罩层透明度
        dialog!!.window?.setDimAmount(0f)
        // 设置宽度
        val params = dialog!!.window?.attributes
        params!!.width = WindowManager.LayoutParams.WRAP_CONTENT
        params!!.height = WindowManager.LayoutParams.WRAP_CONTENT
        params!!.gravity = Gravity.CENTER or Gravity.CENTER_HORIZONTAL
        dialog!!.window?.attributes = params
        //不可点击外部取消
        dialog!!.setCancelable(false)
        dialog!!.setCanceledOnTouchOutside(false)

        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_fragment_anti_pinch_7808, null)
        initUI(rootView)
        LiveEventBus.get(MqttConstant.LIVE_EVENT_BUS_KEY_RECEIVE_MESSAGE2).observe(this, {
            try {
                if (deviceStatusBean.antiPinchCheck != "1") {
                    dismiss()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        })
//        BleHelper.getInstance(activity).registerBleDataCallback(object : BleArmchairCallback {
//            override fun armchairStateChange() {
//                try {
//                    if (MassageArmchair.getInstance().antiPinchSmallLegOutIn == 0 && MassageArmchair.getInstance().antiPinchSmallLegUpDown == 0){
////                        ToastUtils.showShort("消失")
//                        dismiss()
//                        BleHelper.getInstance(activity).unRegisterBleDataCallback(this)
//                    }
//                }catch (e:Exception){
//                    e.printStackTrace()
//                }
//            }
//        })

        return rootView
    }

    private fun initUI(view: View?) {
        view!!.findViewById<TextView>(R.id.tv_jiechu).setOnClickListener {
            DataManager.dataManager.sendCommond(CommmandNum.ANTIPINCHCHECK, "2")
        }
    }
}
